package com.yiyuan.app.aop;


import com.guaike.core.redis.context.ApiContext;
import com.yiyuan.app.entity.annotation.Task;
import com.yiyuan.app.entity.enums.TaskTypeEnum;
import com.yiyuan.app.entity.result.AppUser;
import com.yiyuan.app.service.BaseUtilService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.time.LocalDate;


/**
 * 防止重复提交业务逻辑
 *
 * @Author: zhangxilin
 * @Date: 2022/12/14
 */
@Aspect
@Component
@Order(5)
@Slf4j
@AllArgsConstructor
public class TaskAspect {

    private final BaseUtilService baseUtilService;

    @Pointcut("@annotation(task)")
    public void pointCut(Task task) {
    }

    @Around(value = "pointCut(task)")// 环绕通知
    public Object methodAround(ProceedingJoinPoint point, Task task) throws Throwable {
        AppUser currentUser = ApiContext.getCurrentUser();
        LocalDate today = LocalDate.now();
        TaskTypeEnum taskTypeEnum = task.taskType();
        if (taskTypeEnum.getCode().equals(TaskTypeEnum.daily_task.getCode())) {
            String dailyKey = currentUser.getUserId() + today.toString() + "dailyTask";
            baseUtilService.setDailyTask(dailyKey, currentUser.getUserId(), today);
        } else {
            String key = currentUser.getUserId() + today.toString() + "task";
            baseUtilService.setTask(key, currentUser.getUserId());
        }
        return point.proceed();
    }


}
